/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package weixin.popular.message.handler.defaultimpl;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weixin.popular.bean.message.EventMessage;
import weixin.popular.bean.xmlmessage.XMLMessage;
import weixin.popular.message.handler.MessageHandler;
import weixin.popular.message.handler.MessageService;

/**
 *
 * @author johns
 */
public class DefaultMessageService implements MessageService {
    private Logger logger = LoggerFactory.getLogger(DefaultMessageService.class);
    private BlockingQueue enentMessageQueue = new LinkedBlockingQueue();
    private List<MessageHandler> sortedProviders;
    DefaultMessageService(List<MessageHandler> sortedProviders){
        this.sortedProviders = sortedProviders;
    }
//    private Map<String,Class> providers;
//    private List<String> orders;

    @Override
    public XMLMessage reply(EventMessage message) {
        XMLMessage reply = null;
        for(MessageHandler handler : sortedProviders){
            if(handler.isSupport(message)){
                reply = handler.reply(message);
                break;
            }
        }
        return reply;
    }
    
//    public void sortProviders(){
//        Set<String> keys = providers.keySet();
//        if(keys!=null && keys.size()>1 && orders!=null && orders.size()>1){
//            for(String item : orders ){
//                for(String name :keys){
//                    if(item.equals(name)){
//                        try {
//                            sortedProviders.add((MessageHandlerProvider)(providers.get(name).newInstance()));
//                        } catch (InstantiationException|IllegalAccessException ex) {
//                            logger.error("创建 provider 实例异常，类名为：" + name, ex);
//                        }
//                        providers.remove(name);
//                        break;
//                    }
//                }
//            }
//        }
//    }
//
//    public void setProviders(Map<String,Class> providers) {
//        this.providers = providers;
//    }
//
//    public void setOrders(List<String> orders) {
//        this.orders = orders;
//        this.sortProviders();
//    }
}
